import { SAVEUSERINFO, DELETEUSERINFO } from "../action_types";
const user = localStorage.getItem("user") || false;
const userInfo = JSON.parse(user),
  token = localStorage.getItem("token");

const initState = {
  user: userInfo || {},
  token: token || "",
  isLogin: userInfo && token ? true : false,
};
export default function test(preState = initState, action) {
  const { type, data } = action;
  let newState;
  switch (type) {
    case SAVEUSERINFO:
      newState = { user: data.user, token: data.token, isLogin: true };
      return newState;
    case DELETEUSERINFO:
      newState = { user: "", token: "", isLogin: false };
      return newState;
    default:
      return preState;
  }
}
